package design_pattern.factory_simple;

/** 简单工厂 */
public class SimpleFactory {
	
	/**普通简单工厂*/
	public static Food getFood(String className){
		switch(className){
		case "Chips":
			Chips chips = new Chips();
			return chips;
		case "Apple":
			Apple apple = new Apple();
			return apple;
		default:
			return null;
		}
	}
	
	/**直接使用反射的话，比较方便，适合功能性的对象生产*/
	public static Food getFoodAuto(Class<?extends Food> clazz){
		Food obj = null;
        try {
            obj = (Food) Class.forName(clazz.getName()).newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return obj;
	}

	public static void main(String[] args){
		Food chips = SimpleFactory.getFood("Chips");
		chips.get();
		
		Food apple = SimpleFactory.getFoodAuto(Apple.class);
		apple.get();
	}
}
